<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { BasicTableColumn } from '@/types'
import {
  Supplier,
  SupplierAddType,
  SupplierEditType,
} from '@/types/modules/supplier'
import { formatToDateTime } from '@/utils/dateUtil'
import { useModal } from '@/hooks/system/useModal'
import { BasicModal as BModal } from '@/components/Modal'
import { useI18n } from '@/hooks/web/useI18n'
import { useHttp } from '@/hooks/web/useHttp'
import { useTableList } from '@/hooks/system/useTableList'
import * as api from '@/api/modules/supplier'
import list from './components/list.vue'
import add from './add.vue'
import edit from './edit.vue'
import detail from './detail.vue'

const { t } = useI18n()

const searchParams = reactive({})

let basicData: BasicTableColumn<Supplier>[] = [
  {
    title: '名称',
    dataIndex: 'name'
  },
  {
    title: '联系人',
    dataIndex: 'contactor'
  },
  {
    title: '联系方式',
    dataIndex: 'phone'
  },
]

const { columns, pageParams, tableData, total, getData } =
  useTableList<Supplier>(basicData)

onMounted(() => {
  getDataList()
})

const getDataList = getData(api.getSupplierPage, searchParams)

const deleteDataHttp = useHttp(
  t('system.api.deleteError'),
  t('system.api.deleteSuccess')
)
const hanldeDeleteData = async (supplier: Supplier) => {
  const res = await api.deleteSupplier(supplier.id)
  deleteDataHttp(res, getDataList)
}

const addFormRef = ref()
const {
  bindValue: addBindValue,
  register: addRegister,
  onShow: onShowAdd,
  onClose: onCloseAdd,
  getInnerForm: getAddForm
} = useModal<Supplier>()
const handleAddResult = useHttp(
  t('system.api.addError'),
  t('system.api.addSuccess')
)

const onFinishAdd = async function () {
  const result = await getAddForm<SupplierAddType>(addFormRef)
  if (result) {
    const res = await api.createSupplier(result)
    handleAddResult(res, () => {
      getDataList()
      onCloseAdd()
    })
  }
}

const editFormRef = ref()
const {
  bindValue: editBindValue,
  register: editRegister,
  currentItem: editCurrentItem,
  onShow: onShowEdit,
  onClose: onCloseEdit,
  getInnerForm: getEditForm
} = useModal<Supplier>()
const handleEditResult = useHttp(
  t('system.api.editError'),
  t('system.api.editSuccess')
)

const onFinishEdit = async function () {
  const result = await getEditForm<SupplierEditType>(editFormRef)
  if (result) {
    const res = await api.updateSupplier(result, editCurrentItem.value?.id)
    handleEditResult(res, () => {
      getDataList()
      onCloseEdit()
    })
  }
}

const {
  bindValue: detailBindValue,
  register: detailRegister,
  currentItem: detailCurrentItem,
  onShow: onShowDetail,
  onClose: onCloseDetail
} = useModal<Supplier>()
</script>

<template>
  <div class="page-wrapper">
    <a-card class="table-card">
      <list v-bind="{ pageParams, total, columns, tableData }" title="供应商" @add="onShowAdd" @refresh="getDataList">
        <template #operate="row">
          <a type="link" @click="onShowDetail(row)">{{ t('system.detail') }}</a>
          <a-divider type="vertical" />
          <a type="link" @click="onShowEdit(row)">{{ t('system.edit') }}</a>
          <a-divider type="vertical" />
          <a-popconfirm :title="t('system.deletePrompt')" placement="left" @confirm="hanldeDeleteData(row)">
            <a type="link" class="ap-action-danger-btn">{{
                t('system.delete')
            }}</a>
          </a-popconfirm>
        </template>
      </list>
    </a-card>

    <b-modal v-bind="addBindValue" :title="t('system.modal.add')" @register="addRegister" @ok="onFinishAdd">
      <add ref="addFormRef" />
    </b-modal>

    <b-modal v-bind="editBindValue" :title="t('system.modal.edit')" @register="editRegister" @ok="onFinishEdit">
      <edit ref="editFormRef" :item-data="editCurrentItem" />
    </b-modal>

    <b-modal v-bind="detailBindValue" :footer="null" :title="t('system.modal.detail')" @register="detailRegister"
      @cancel="onCloseDetail">
      <detail :item-data="detailCurrentItem" />
    </b-modal>
  </div>
</template>

<style lang="less" scoped>
.page-wrapper {
  padding: 20px;

  .table-card {
    margin-top: 20px;
  }
}

.drawer-footer {
  text-align: right;

  button {
    margin-left: 8px;
  }
}
</style>
